package com.example.movie.utils

import android.content.Context
import com.example.movie.ui.MainActivity
import kotlin.reflect.KClass

/**
 * 保存context上下文,以便在其他地方方便获取context
 *
 * @author swift
 */
object ContextHolder {

    private val containers: HashMap<String, Context> = hashMapOf()

    fun addContext(context: Context) {
        context::class.qualifiedName?.let {
            containers[it] = context
        }
    }

    @SuppressWarnings("unchecked")
    fun <T : Context> from(cls: KClass<T>): T {
        return containers[cls.qualifiedName] as T
    }

    fun getMainContext(): MainActivity {
        return from(MainActivity::class)
    }

}